import request, { SucMessage } from '@/utils/request';

export interface BaseFileProps {
  recTypeId: number | string; // 关联业务类型id
  recId: number | string; // 关联业务对象id
  subRecTypeId: number; // 关联子业务类型id
  subRecId: number; // 关联子业务对象id
}

interface GetViewProps {
  dataId: string;
  dataType: 'CustomView' | 'CompanyView';
}
interface UpdateViewProps extends GetViewProps {
  views: any[];
}

/**
 * 获取视图列表(customView,companyView)
 * @param {object} params
 */
export async function getViewsApi(params: GetViewProps): Promise<any[]> {
  let url = '/accountUserSelf/getUserData';
  if (params.dataType === 'CompanyView') {
    url = '/companyData/getCompanyData';
  }
  let res = [];

  try {
    res = await request(url, {
      method: 'POST',
      data: params,
      serviceId: 'fwSecurity',
    });
  } catch (error) {}

  return res?.bigData ? JSON.parse(res.bigData) : [];
}

/**
 * 更新视图列表(customView,companyView)
 * @param ids 文件id列表
 * @param showSuccess 是否显示成功提示
 */
export function updateViewsApi(params: UpdateViewProps) {
  const { dataType, dataId, views } = params;
  let url = '/accountUserSelf/setUserData';
  if (params.dataType === 'CompanyView') {
    url = '/companyData/setCompanyData';
  }
  return request(
    url,
    {
      method: 'POST',
      data: {
        dataId,
        dataType,
        bigData: JSON.stringify(views),
      },
      serviceId: 'fwSecurity',
    },
    {
      showSuccess: true,
      successMessage: SucMessage.operateMes,
    },
  );
}
